/**
 * APP首页
 * @Author : andy.wangzhh
 * @Timestamp : 2016-06-26
 */
var deviceone = require("deviceone");
var app = sm("do_App");
var page = sm("do_Page");
var global = sm("do_Global");
var hashdata = mm("do_HashData");
var nf = sm("do_Notification");
var allpage = require("helper/allpage");
var openHelper = require("helper/open");
var userUtil = require("helper/user");
var deviceUtil = require("helper/device");
var qqCacher = require("helper/qqCacher");
var imHelper = require("helper/im");
var helperVote = require("helper/vote");
var httpSilence = require("helper/httpSilence");
var urlMall = require("url/mall");
var urlUser = require("url/user");
var userUtil = require("helper/user");

var rootview = ui("$");
var do_ALayout_4=ui("do_ALayout_4");

var maskbtn=ui("maskbtn");

page.on("back", function(){
	deviceUtil.home();
});

page.on("loaded", function(){
	/*if(null==qqCacher.honorPageIsShow()||''==qqCacher.honorPageIsShow()){
		ui("maskHonor").visible=true;
		qqCacher.honorPageIsShow(1);
	}else{
		ui("maskHonor").visible=false;
	}*/
	imHelper.cacheUnreadMsgCount4Andriod();
});

do_ALayout_4.on("touch", function(){
	ui("maskHonor").visible=false;
	openHelper.open("source://view/honor/index/index.ui");
});

maskbtn.on("touch", function(){
	ui("maskHonor").visible=false;
	openHelper.open("source://view/honor/index/index.ui");
});

var mainShower = ui("mainShower");
var mains = 
	[
	 	{"id":"main0","path":"source://view/index/index.ui"},//0社区
        //{"id":"main1","path":"source://view/circle/main.ui"},
		{"id":"main1","path":"source://view/discover/main.ui"},//1发现
		{"id":"main2","path":"source://view/mall/mallMain.ui"},//2商城
		{"id":"main3","path":"source://view/user/main2.ui"} //3我的
	];
mainShower.addViews(mains);
mainShower.showView("main0");
var footIndex = 0;

var footImg0 = ui("footImg0");
var footImg1 = ui("footImg1");
var footImg2 = ui("footImg2");
var footImg3 = ui("footImg3");
var footImgArr = [footImg0,footImg1,footImg2,footImg3];

var footLabel0 = ui("footLabel0");
var footLabel1 = ui("footLabel1");
var footLabel2 = ui("footLabel2");
var footLabel3 = ui("footLabel3");
var footLabelArr = [footLabel0,footLabel1,footLabel2,footLabel3];

var foot0 = ui("foot0");
var foot1 = ui("foot1");
var foot2 = ui("foot2");
var foot3 = ui("foot3");
var footArr = [foot0,foot1,foot2,foot3];
app.on("fireUserLogout",function(d){
	userUtil.userLogo("");
	userUtil.userNick("");
	userUtil.userId("");
	userUtil.loginToken("");
	foot0.fire("touch");
});

footArr.forEach(function(foot,tabIndex){
	foot.on("touch", function(data,e){
		var targetSource = mains[tabIndex].path;
		if(targetSource in allpage.uis){
			var uiVar = allpage.uis[targetSource];
			if(uiVar.login==true && (userUtil.loginToken()==="undefined"||userUtil.loginToken()==="")){
				nf.toast("请先登录再操作");
				openHelper.openLogin();
				return;
			}
		}
		if(footIndex !== tabIndex){
			footImgArr[footIndex].source = "source://image/footer/"+footIndex+".png";
			footLabelArr[footIndex].fontColor = "949EAAFF";
			footImgArr[tabIndex].source = "source://image/footer/"+tabIndex+"_on.png";
			footLabelArr[tabIndex].fontColor = "FF7E00FF";
			mainShower.showView("main"+tabIndex);
			fire2reflashData(tabIndex);
		}
		footIndex = tabIndex;
	});
});
mainShower.on("viewChanged", function(data, e) {
});
/*
 * 菜单弹出
 */
var popmenu = rootview.add("menupop", "source://view/reuse/menu/menu.ui", 0, 0);
var popmenuf = ui("menupop");
page.on("pageMenuBox",function(data){
	popmenuf.fire("popmenuSJ",data);
});

popmenuf.on("popmenuItem",function(data){
	popmenuf.fire("popmenuClose");//关闭菜单
	var touchIndex = parseInt(data);
	page.fire("pageMenuItem"+footIndex,touchIndex);
});


var popnav = rootview.add("navpop", "source://view/community/index/header/menu.ui", 0, 0);
var popnavf = ui("navpop");
page.on("pageNavBox",function(data){
	popnavf.fire("popnavSJ",data);
});

page.on("popnavItem",function(){
	popnavf.fire("popnavClose");
});

//菜单弹出
var addMenu = ui("addMenu");
var mainMenu = rootview.add("mainMenu", "source://view/reuse/menu/index.ui", 0, 0);
var mainMenuf = ui("mainMenu");
var menus = [
    {"menuBg":"fe7e0aFF","menuTitle":"邻里动态","menuIcon":"source://image/menu/m_01.png","type":"addComMessage"},
	{"menuBg":"fabb17FF","menuTitle":"圈子动态","menuIcon":"source://image/menu/m_02.png","type":"addCircleTopic"},
	{"menuBg":"8dc21fFF","menuTitle":"扫二维码","menuIcon":"source://image/menu/m_07.png","type":"linkCode"},
	{"menuBg":"ff5305FF","menuTitle":"创建圈子","menuIcon":"source://image/menu/m_03.png","type":"addCircle"},
	{"menuBg":"5579b6FF","menuTitle":"发布活动","menuIcon":"source://image/menu/m_04.png","type":"addActivity"},
	{"menuBg":"ba4fb3FF","menuTitle":"圈子投票","menuIcon":"source://image/menu/m_05.png","type":"addVote"}
	  
];
addMenu.on("touch", function() {	
	mainMenuf.fire("mainMenuSJ",menus);
});

mainMenuf.on("mainMenuItem",function(data){
	var touchIndex = parseInt(data);
	var actionType = menus[touchIndex].type;
	if(actionType=="addActivity"){//发布活动
		openHelper.open("source://view/activity/add/circle/mainNew.ui");
	}else if(actionType=="addCircle"){//创建圈子
		openHelper.open("source://view/circle/base/manage/circleAdd.ui");
	}else if(actionType=="addCircleTopic"){//发布圈子动态
		//nf.toast("发布--圈子动态");
		openHelper.open("source://view/circle/select/selectRadio.ui",{"forWhat":"addCircleTopic"});
	}else if(actionType=="addComMessage"){
		openHelper.open("source://view/community/message/add/main.ui");
	}
	//二维码链接
	else if(actionType=="linkCode"){
		openHelper.open("source://view/public/barcode/scan.ui");
	
	}else if(actionType=="addVote"){
		openHelper.open("source://view/circle/select/selectRadio.ui",{"forWhat":"addCircleVote"});
	}
});
/**
 * 刷新对应页面的数据
 * @param type
 */
function fire2reflashData(index){
	if(index==0){//社区
		page.fire("index_dynamic_tab_touch");
	}else if(index==1){//发现
		page.fire("index_discover_tab_touch");
	}else if(index==2){//商城
		page.fire("index_mall_tab_touch");
	}else if(index==3){//我的
		page.fire("index_myself_tab_touch");
	}
}

//处理消息红点是否提示
function doSomeThing2MsgTip(){
	var unreadCount = imHelper.getMyUnReadCount();
	//deviceone.print(unreadCount,"unreadCount-未读消息数量");
	if(parseInt(unreadCount) > 0){
		app.fire("qq_fire_show_msg_tip");//显示消息红点
	}else{
		app.fire("qq_fire_hide_msg_tip");//隐藏消息红点
	}
}

page.on("result", function(data) {
	//deviceone.print("index....result");
	imHelper.addMsgListener();//从新回到这个页面时一定要重新监听IM,否则收不到消息
	if(data.action == "selectOneCircleBack"){
		var circleId = data.id;
		if(circleId=="" && circleId == null && circleId =="undefined"){
			return;
		}
		if(data.forWhat =="addCircleTopic"){//发布圈子话题
			openHelper.open("source://view/circle/topic/manage/topicAdd.ui",{circleId:circleId});
		}else if(data.forWhat =="addCircleVote"){//发布圈子投票
			openHelper.open("source://view/vote/add/main.ui",{circleId:circleId,bizzType:helperVote.bizzType.CIRCLE});
		}
	}
	doSomeThing2MsgTip();//处理消息红点是否提示
	if(0==footIndex&&null!=data){//添加圈子话题
		if(null!=data.fromPage&&data.fromPage=="message_add"){//添加社区大喇叭信息
			page.fire("index_community_tab_touch",{"brush":true});
		}else if(data.topic_add){//添加圈子话题
			page.fire("index_community_tab_touch",{"brush":true});
		}else if(data.circle_add){//添加圈子
			page.fire("index_community_tab_touch",{"brush":true});
		}else if(null!=data.fromPage&&data.fromPage=="activity_add"){//添加活动
			page.fire("index_community_tab_touch",{"brush":true});
		}else if(data.topic_add_vote){//添加投票
			page.fire("index_community_tab_touch",{"brush":true});
		}else if(null!=data.fromPage&&data.fromPage=="distance"){//附近小区
			page.fire("index_community_tab_touch");
		}else if(null!=data.fromPage&&data.fromPage=="search"){//搜索小区
			page.fire("index_community_tab_touch");
		}else if(null!=data.fromPage&&data.fromPage=="login"){//登陆
			page.fire("index_community_tab_touch",{"brush":true});
		}else if(null!=data.fromPage&&data.fromPage=="loginOut"){//退出
			page.fire("index_community_tab_touch",{"brush":true});
		}else if(data.isAddCommentPraise&&null!=data.updateData){//从详情返回，有点赞、评论动作、大喇叭
			page.fire("index_community_tab_comment_praise",{updateData:data.updateData});
		}else if(data.isCircleTopicUpdate){//从详情返回，有点赞、评论动作
			deviceone.print("isCircleTopicUpdate"+data.isCircleTopicUpdate);
			page.fire("index_community_tab_comment_praise_circleTopic",{circleTopicData:data});
		}else if(null!=data.fromPage&&data.fromPage=="honorIndex"){//添加社区大喇叭信息
			page.fire("index_community_sign_brush",{"brush":false,signCommunity:data.signCommunity});
		}
	}
	if(1==footIndex&&null!=data){//发现
		if(null!=data.fromPage&&data.fromPage=="search"){//搜索小区
			page.fire("index_discover_tab_touch");
		}
	}
});

app.on("fireShowView",function(index){
	footArr[index].fire("touch");
});

ui("maskHonor").on("touch","",300,function(){
	this.visible = false;
});


//动态搜索
var regionSearch = rootview.add("regionSearch", "source://view/index/search/region.ui", 0, 0);
var regionSearchUI = ui("regionSearch");
page.on("regionPageShow",function(data){
	regionSearchUI.fire("regionShow",data);
});
var topicSearch = rootview.add("topicSearch", "source://view/index/search/topic.ui", 0, 0);
var topicSearchUI = ui("topicSearch");
page.on("topicPageShow",function(data){
	topicSearchUI.fire("topicShow",data);
});

